package com.study.zzedu.ui.courseintroduce;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.androidkun.xtablayout.XTabLayout;
import com.study.zzedu.R;
import com.study.zzedu.adapter.EvaluateAdapter;
import com.study.zzedu.adapter.TabAdapter;
import com.study.zzedu.adapter.TabForCourseAdapter;
import com.study.zzedu.base.BaseMvpActivity;
import com.study.zzedu.entity.EvaluateItemModel;
import com.study.zzedu.ui.evaluate.EvaluateActivity;
import com.study.zzedu.utils.AppConstant;
import com.study.zzedu.utils.LogUtil;
import com.study.zzedu.utils.ToastUtil;
import com.study.zzedu.widget.SpacesItemDecoration;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CourseIntroduceActivity extends BaseMvpActivity<CourseIntroduceContract.IView,CourseIntroducePresenter> implements CourseIntroduceContract.IView {
    private XTabLayout tabLayout;
    private ViewPager viewPager;
    private List<String> list;

    public static String introduceContent="hahaha";
    public static JSONObject teachingPlan;
    @Override
    protected int getLayoutID() {
        return R.layout.activity_course_introduction;
    }

    @Override
    protected CourseIntroducePresenter createPresenter() {
        return new CourseIntroducePresenter();
    }

    @Override
    protected CourseIntroduceContract.IView createView() {
        return this;
    }

    @Override
    protected void initView() {
        findViewById(R.id.rl_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        viewPager = findViewById(R.id.viewpager1);
        tabLayout = findViewById(R.id.tab1);
    }

    @Override
    protected void initData() {
        list = new ArrayList<>();
        list.add("课程简介");
        list.add("课程大纲");
        list.add("教学计划");

        getPresenter().getAllData(CourseIntroduceActivity.this, AppConstant.userCode,AppConstant.courseCode);
    }

    @Override
    protected void initListener() {}

    @Override
    public void getAllData(String data) {
        try {
            JSONObject jObject = new JSONObject(data);
            JSONArray jsonArray = jObject.getJSONArray("data");
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject content = jsonArray.getJSONObject(i).getJSONObject("info");
                JSONObject detail = jsonArray.getJSONObject(i).getJSONObject("teachingPlan");
                introduceContent = content.getString("info");
                teachingPlan = detail;
            }

            if (!(jsonArray.length() > 0)) {
                ToastUtil.showShortToast("无数据");
                return;
            }else {
                /*设置Adapter*/
                viewPager.setAdapter(new TabForCourseAdapter(getSupportFragmentManager(), list));
                viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    }

                    @Override
                    public void onPageSelected(int position) {
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {
                    }
                });
                /*Tab与ViewPager绑定*/
                tabLayout.setupWithViewPager(viewPager);
            }
        } catch (Exception e) {
            LogUtil.e("Exception", e.getMessage());
        }
    }

    @Override
    public void getData(String data) {

    }

    @Override
    public void showError(String error) {
        if (null != error && !TextUtils.isEmpty(error)) {
            ToastUtil.showShortToast(error);
        }
    }
}